package com.paypalpay.turntointerfaceservice;

import com.paypalpay.entity.common.DeviceRealTimeInformation;
import com.paypalpay.pojo.Constant;
import com.paypalpay.pojo.request.RequestBody.RenewPackagesRequestBody;
import com.paypalpay.pojo.request.RequestHeader;
import com.paypalpay.pojo.request.subRequest.RenewPackagesRequest;
import com.paypalpay.pojo.response.subResponse.RenewPackagesResponse;
import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
import com.paypalpay.util.JaxbUtil;
import com.paypalpay.util.wx.HttpClientTool;

public class TurnToRenewPackagesService {

    public DeviceRealTimeInformation renewPackage(String orderId,String deviceType, String renewPeriod, String secret, String partnerCode) {
        try {
            RenewPackagesRequestBody body = new RenewPackagesRequestBody();
            body.setOrderId(orderId);
            body.setDeviceType(deviceType);
            body.setRenewPeriod(renewPeriod);

            // 请求参数拼接
            RenewPackagesRequest request = new RenewPackagesRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_renewPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            System.out.println("签名是：" + sign);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(RenewPackagesRequest.class).toXml(request, "utf-8");
            System.out.println("请求内容：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            System.out.println("返回结果：" + result);

            RenewPackagesResponse responseObj = new JaxbUtil(RenewPackagesResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                System.out.println("信息是什么？" + retCode);
                // 成功:北京回传的orderId
                String orderCode = responseObj.getBody().getOrderId();
                String expireDate = responseObj.getBody().getOrderExpireDate();

                DeviceRealTimeInformation user = new DeviceRealTimeInformation();
                user.setOrderCode(orderCode);
                user.setExpireDate(expireDate);
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;

            } else {
                DeviceRealTimeInformation user = new DeviceRealTimeInformation();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;

    }
}
